package net.uhuijia.user.userwxauth.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.uhuijia.appbase.service.CrudService;
import net.uhuijia.constants.DBConst;
import net.uhuijia.user.userwxauth.persistence.dao.UserWxauthDao;
import net.uhuijia.user.userwxauth.persistence.model.UserWxauth;
import net.uhuijia.user.userwxauth.persistence.model.UserWxauthExample;
import net.uhuijia.user.userwxauth.service.UserWxauthService;
import net.uhuijia.utils.StringHelper;


@Service
@Transactional
public class UserWxauthServiceImpl
     extends CrudService<UserWxauthDao,UserWxauth,UserWxauthExample>
     implements UserWxauthService{

	
	@Override
	public UserWxauth getByAppidAndOpenid(String appid, String openid) {
		UserWxauth wxauth = new UserWxauth();
		wxauth.setIsEffective(DBConst.TRUE);
		wxauth.setAuthAppid(appid);
		wxauth.setAuthOpenid(openid);
		return mapper.getByAppidAndOpenid(wxauth);
	}

	@Override
	public String addWxauth(UserWxauth param) {
		UserWxauth wxauth = this.getByAppidAndOpenid(param.getAuthAppid(),param.getAuthOpenid());
		if( null == wxauth ){
			this.initDefalutFields(param);
			this.add(param);
		}else{
			if( StringHelper.isBlank(wxauth.getAuthUnionid()) && StringHelper.isNotBlanks(param.getAuthUnionid()) ){
				wxauth.setAuthUnionid(param.getAuthUnionid());
				this.edit(wxauth);
			}
		}
		return SUCCESS;
	}


}